var app = getApp();
var api = require("../../utils/api.js");
var util= require("../../utils/libs.js");
Page({
  data: {
    title:'',
    rest_num:0,
    text_number:0,
    content:"",
    is_draft:0,
    id:null,
    uid:'',
    tid:null,
    images:""
  },
  getValue(event){
    let that = this
    let values = event.detail.value;
    that.setData({
      title: values
    })
   },
   gettextarea(event){
    let that = this
    let values = event.detail.value;
    console.log(values)
    that.setData({
      content: values,
      rest_num:values.length
    })
   },
  onLoad (options) {
    let that = this
    let  uid = wx.getStorageSync('uid') 
    console.log(options)
     that.setData({
      tid:options.id,
      id:options.id,
      uid:uid,
      text_number:options.textNumber
    })
    // 页面初始化 options为页面跳转所带来的参数
  },
  closeImages(){
    this.setData({
      images:''
    })
  },
  chooseImageTap (){
    let _this = this;
    wx.showActionSheet({
     itemList: ['从相册中选择', '拍照'],
     itemColor: "#f7982a",
     success: function(res) {
     if (!res.cancel) {
     if(res.tapIndex == 0){
     _this.chooseWxImage('album')
     }else if(res.tapIndex == 1){
     _this.chooseWxImage('camera')
     }
     }
     },
     fail: function(res) {
      console.log(res.errMsg)
    }
    })
},
chooseWxImage:function(type){
  let _this = this;
  wx.chooseImage({
   sizeType: ['original', 'compressed'],
   sourceType: [type],
   success: function (res) {
   _this.setData({
   logo: res.tempFilePaths[0],
   })
   app.upload_file("https://bucuo.jikeyun.net/api/upload",
    _this.data.logo,"upload",{},function success(res){
        res = JSON.parse(res)
      if(res.error_code === 0){
        _this.setData({
          images:res.data.file
        })
      }
    })
 }
})
},
toFabu(){
  let that = this
  if (that.data.title=="") {
    wx.showToast({
      title:'请输入文章题目',
      icon:'none',
      duration: 2000,
    })
    return;
  }
  if (this.data.content=="") {
    wx.showToast({
      title:'请输入文章内容',
      icon:'none',
      duration:2000
    })
    return;
  }
  wx.showToast({
    title:'发布后不能删除',
    icon:'none',
    duration:2000
  })
  setTimeout(function(){
    util.request(api.topicArticleAdd,{
      tid:that.data.tid,
      uid:that.data.uid,
      title:that.data.title,
      content:that.data.content,
      is_draft:that.data.is_draft
    },
    "POST").then(function (res) {
      if(res.error_code === 0){
       wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000,
        success:function(){
          wx.navigateTo({
            url:"/pages/articledetail/articledetail?id="+res.data.id
          })
        }
      })
    }
  },2000)
  })
},
  toAdd(){
    let that = this
    that.setData({
      is_draft:1
    })
    if(this.data.title==""){
      wx.showToast({
        title:'请输入文章题目',
        icon:'none',
        duration: 2000,
      })
      return ;
    }
    if(this.data.content=""){
      wx.showToast({
        title:'请输入文章内容',
        icon: 'none',
        duration: 2000,
      })
      return ;
    }
  util.request(api.topicArticleAdd,{
    tid:that.data.tid,
    uid:that.data.uid,
    title:that.data.title,
    content:that.data.content,
    is_draft:that.data.is_draft
  },
  "POST")
  .then(function (res) {
    if(res.error_code === 0){
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000,
        success:function(){
          wx.navigateTo({
            url:"/pages/titlearticle/titlearticle?id="+res.data.id 
          })
        }
      })  
    }
  })
  }
})
